package com.example.ware.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.ware.annotation.Token;
import com.example.ware.dao.AnnouncementMapper;
import com.example.ware.model.Spage.ShowPage;
import com.example.ware.model.po.Announcement;
import com.example.ware.model.responseData.ResponseData;
import com.example.ware.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    @Resource
    AnnouncementService announcementService;

    @Resource
    AnnouncementMapper announcementMapper;

//    发布公告
    @PostMapping("/publish")
    @Token
    public ResponseData publish(@RequestBody Announcement announcement) {
        Date date = new Date();
        announcement.setCreatetime(date);
        announcement.setUpdatetime(date);
        announcement.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        announcement.setAdminid("111111111");
        boolean i = announcementService.save(announcement);

        if (i) {
            return new ResponseData(null, true, "发布成功", 200);
        } else {
            return new ResponseData(null, false, "发布失败", 201);

        }
    }


//    删除指定的公告
    @DeleteMapping("/del")
    @Token
    public ResponseData delAnnList(String id){
        boolean i=announcementService.removeById(id);
        if(i){
            return new ResponseData(null,true,"删除成功",200);
        }else{
            return new ResponseData(null,true,"删除失败",201);
        }
    }

//    修改指定的公告
    @PutMapping("/update")
    @Token
    public ResponseData updateAnn(@RequestBody Announcement announcement){
        UpdateWrapper<Announcement> uw=new UpdateWrapper<>();
        uw.eq("id", announcement.getId());
        uw.set("content", announcement.getContent());
        Date date = new Date();
        uw.set("updatetime",date);

        boolean i=announcementService.update(uw);
        if (i) {
            return new ResponseData(null, true, "修改成功", 200);
        } else {
            return new ResponseData(null, false, "修改失败", 201);

        }

    }

//    获取公告的列表
    @GetMapping("/getList")
    public ResponseData getAnnList(Integer page,Integer pageSize,@RequestParam(defaultValue = "1")String index, @RequestParam(defaultValue = "2")String index2){

        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;
        showPage.setPageNum(page);
        //总数据条数
        showPage.setSize(announcementService.list().size());
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);


        if(index.equals("1")){
            if(index2.equals("1")){
                showPage.setData(announcementMapper.getAnnList(pageBegin,pageSize,"ASC","updatetime"));
            }else{
                showPage.setData(announcementMapper.getAnnList(pageBegin,pageSize,"DESC","updatetime"));
            }
        }else{
            if(index2.equals("1")){
                showPage.setData(announcementMapper.getAnnList(pageBegin,pageSize,"ASC","createtime"));

            }else{
                showPage.setData(announcementMapper.getAnnList(pageBegin,pageSize,"DESC","createtime"));

            }
        }


        if(showPage.getData()==null){
            return new ResponseData(null, false, "出问题了,请联系程序员", 201);
        }else{
            return new ResponseData(showPage, true, "获取成功", 200);
        }

    }

}
